﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Collections.Generic;


namespace TRPOP.CMS.Web.Controls
{
    public partial class UC_Pager : System.Web.UI.UserControl
    {
        #region Private Instances

        private int p_intTotalPages = 0;
        private int p_intDisplayedPages = 11;
        private int p_intRecordCount = 0;
        private int p_intPageSize = 20;
        //private string  p_strKey = "page";
        private string p_strCurrentURL = "";
        private string p_strCurrentQueryString = "";
        private string p_strCurrentPath = "";
        public int p_intCurrentPage = 0;
        private bool p_blShowRecordCount = false;
        private bool p_blShowPageSize = false;
        private bool p_blShowCurrentPage = false;
        protected static readonly object EventPageChanged = new object();
        #endregion

        #region Public Properties

        public int TotalPages
        {
            get
            {
                return p_intTotalPages;
            }
            set
            {
                p_intTotalPages = value;
            }
        }

        public int RecordCount
        {
            get
            {
                return p_intRecordCount;
            }
            set
            {
                p_intRecordCount = value;
            }
        }

        public int PageSize
        {
            get
            {
                return p_intPageSize;
            }
            set
            {
                p_intPageSize = value;
            }
        }

        public int DisplayedPages
        {
            get
            {
                return p_intDisplayedPages;
            }
            set
            {
                p_intDisplayedPages = value;
            }
        }

        public int CurrentPage
        {
            get
            {
                GetCurrentPageNo();
                return p_intCurrentPage;
            }
           
        }
        /// <summary>
        /// 显示总记录数
        /// </summary>
        public bool ShowRecordCount
        {
            get { return p_blShowRecordCount; }
            set { p_blShowRecordCount = value; }
        }
        /// <summary>
        /// 显示每页显示多少条记录
        /// </summary>
        public bool ShowPageSize
        {
            get { return p_blShowPageSize; }
            set { p_blShowPageSize = value; }
        }

        /// <summary>
        /// 显示当前页/总页数
        /// </summary>
        public bool ShowCurrentPage
        {
            get { return p_blShowCurrentPage; }
            set { p_blShowCurrentPage = value; }
        }
        #endregion

        #region Event Handlers
        /// <summary>
        /// 定义Onclick
        /// </summary>
        /// <param name="e"></param>
        protected virtual void OnPageChange(EventArgs e)
        {
            EventHandler clickHandler = (EventHandler)Events[EventPageChanged];
            if (clickHandler != null)
            {
                clickHandler(this, e);
            }
        }

        /// <summary>
        /// 引发PostBack事件
        /// </summary>
        /// <param name="eventArgument"></param>
        public void RaisePostBackEvent(string eventArgument)
        {
            OnPageChange(new EventArgs());
        }


        #endregion

        private void GetCurrentPageNo()
        {
            try
            {
                p_intCurrentPage = int.Parse(Request.QueryString["page"].ToString());
            }
            catch
            { }
            if (p_intCurrentPage <= 0)
            {
                p_intCurrentPage = 1;
            }
        }

        protected int getCurrentPageNo()
        {
            return p_intCurrentPage;
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            Reader();
        }

        protected void Reader()
        {
            GetCurrentPageNo();
            if (!p_intPageSize.Equals(0))
            {
                p_intTotalPages = p_intRecordCount % p_intPageSize == 0 ? p_intRecordCount / p_intPageSize : p_intRecordCount / p_intPageSize + 1;
            }
            if (p_intCurrentPage > p_intTotalPages)
            {
                p_intCurrentPage = p_intTotalPages;
            }
            else if (p_intCurrentPage < 1)
            {
                p_intCurrentPage = 1;
            }

            p_strCurrentURL = HttpContext.Current.Request.Url.AbsoluteUri;
            p_strCurrentPath = HttpContext.Current.Request.Url.Host;
            p_strCurrentQueryString = HttpContext.Current.Request.Url.Query;

            bool queryStringIsEmpty = false;
            int offset = p_intDisplayedPages / 2 - 1;

            if (p_strCurrentQueryString.Length == 0)
            {
                queryStringIsEmpty = true;
            }

            TableRow row = new TableRow();
            //row.CssClass = "smalltxt";
            this.Table1.Rows.Add(row);
            if (ShowCurrentPage)
            {
                TableCell cell = new TableCell();
                cell.Text = " &nbsp;当前在第" + CurrentPage + "页/共"+TotalPages+"页&nbsp; ";
                row.Cells.Add(cell);
            }

            if (ShowPageSize)
            {
                TableCell cell = new TableCell();
                cell.Text = " &nbsp;每页显示" + PageSize + "条记录&nbsp; ";
                row.Cells.Add(cell);
            }

            if (ShowRecordCount)
            {
                TableCell cell = new TableCell();
                cell.Text = " &nbsp;共" + RecordCount + "条记录&nbsp; ";
                row.Cells.Add(cell);
            }

            //TableCell cellPageInfo = new TableCell();
            //cellPageInfo.Text = currentPageNo + "/" + p_intTotalPages+"页";
            //cellPageInfo.CssClass = "this-page";
            //row.Cells.Add(cellPageInfo);

            if (p_intTotalPages <= p_intDisplayedPages)
            {
                for (int i = 1; i <= p_intTotalPages; i++)
                {
                    TableCell cell = new TableCell();
                    HyperLink link = new HyperLink();
                    link.EnableTheming = true;
                    link.Text = i.ToString();
                    if (p_intCurrentPage == i)
                    {
                        link.CssClass = "now-page";
                    }
                    else
                    {
                        link.NavigateUrl = getNewURL(i);
                    }
                    cell.Controls.Add(link);
                    row.Cells.Add(cell);
                }
            }
            else
            {
                if (queryStringIsEmpty == true || p_intCurrentPage == 1)
                {
                    for (int i = 1; i <= p_intDisplayedPages; i++)
                    {
                        TableCell cell = new TableCell();
                        HyperLink link = new HyperLink();
                        link.EnableTheming = true;
                        if (i < p_intDisplayedPages - 1)
                        {
                            link.Text = i.ToString();
                            if (i == p_intCurrentPage)
                            {
                                link.CssClass = "now-page";
                            }
                            else
                            {
                                link.NavigateUrl = getNewURL(i);
                            }
                        }
                        else if (i == p_intDisplayedPages - 1)
                        {
                            link.Text = "..." + Convert.ToString(p_intTotalPages);
                            link.NavigateUrl = getNewURL(p_intTotalPages);
                        }
                        else if (i == p_intDisplayedPages)
                        {
                            //link.ImageUrl = "~/Images/next1.gif";
                            //link.SkinID="PagerNext";
                            link.Text = "下一页";
                            link.CssClass = "page-next";
                            link.NavigateUrl = getNewURL(p_intCurrentPage + 1);
                        }
                        cell.Controls.Add(link);
                        row.Cells.Add(cell);
                    }
                }
                else if (p_intCurrentPage > 1 && p_intCurrentPage <= offset)
                {
                    for (int i = 1; i <= p_intDisplayedPages; i++)
                    {
                        TableCell cell = new TableCell();
                        HyperLink link = new HyperLink();
                        link.EnableTheming = true;
                        if (i == 1)
                        {
                            //link.ImageUrl = "~/Images/fore.gif";
                            //link.SkinID="PagerFore";
                            link.Text = "上一页";
                            link.CssClass = "page-fore";
                            link.NavigateUrl = getNewURL(p_intCurrentPage - 1);
                        }
                        else if (i > 1 && i < p_intDisplayedPages - 1)
                        {
                            link.Text = Convert.ToString(i - 1);
                            if (p_intCurrentPage == i - 1)
                            {
                                link.CssClass = "now-page";
                            }
                            else
                            {
                                link.NavigateUrl = getNewURL(i - 1);
                            }
                        }
                        else if (i == p_intDisplayedPages - 1)
                        {
                            link.Text = "..." + Convert.ToString(p_intTotalPages);
                            link.NavigateUrl = getNewURL(p_intTotalPages);
                        }
                        else if (i == p_intDisplayedPages)
                        {
                            //link.ImageUrl = "~/Images/next1.gif";
                            //link.SkinID = "PagerNext";
                            link.Text = "下一页";
                            link.CssClass = "page-next";
                            link.NavigateUrl = getNewURL(p_intCurrentPage + 1);
                        }
                        cell.Controls.Add(link);
                        row.Cells.Add(cell);
                    }
                }
                else if (p_intCurrentPage > offset && p_intCurrentPage <= p_intTotalPages - offset)
                {
                    for (int i = 1; i <= p_intDisplayedPages; i++)
                    {
                        TableCell cell = new TableCell();
                        HyperLink link = new HyperLink();
                        link.EnableTheming = true;
                        if (i == 1)
                        {
                            //link.ImageUrl = "~/Images/fore.gif";
                            //link.SkinID = "PagerFore";
                            link.Text = "上一页";
                            link.CssClass = "page-fore";
                            link.NavigateUrl = getNewURL(p_intCurrentPage - 1);
                        }
                        else if (i == 2)
                        {
                            link.Text = "1...";
                            link.NavigateUrl = getNewURL(1);
                        }
                        else if (i > 2 && i < p_intDisplayedPages - 1)
                        {
                            if (p_intDisplayedPages % 2 == 0)
                            {
                                link.Text = Convert.ToString(p_intCurrentPage - (p_intDisplayedPages - (p_intDisplayedPages % 2)) / 2 + i);

                                if (p_intCurrentPage == p_intCurrentPage - (p_intDisplayedPages - (p_intDisplayedPages % 2)) / 2 + i)
                                {
                                    link.CssClass = "now-page";
                                }
                                else
                                {
                                    link.NavigateUrl = getNewURL(p_intCurrentPage - (p_intDisplayedPages - (p_intDisplayedPages % 2)) / 2 + i);
                                }
                            }
                            else
                            {
                                link.Text = Convert.ToString(p_intCurrentPage - (p_intDisplayedPages - (p_intDisplayedPages % 2)) / 2 + i - 1);
                                if (p_intCurrentPage == p_intCurrentPage - (p_intDisplayedPages - (p_intDisplayedPages % 2)) / 2 + i - 1)
                                {
                                    link.CssClass = "now-page";
                                }
                                else
                                {
                                    link.NavigateUrl = getNewURL(p_intCurrentPage - (p_intDisplayedPages - (p_intDisplayedPages % 2)) / 2 + i - 1);
                                }
                            }
                        }
                        else if (i == p_intDisplayedPages - 1)
                        {
                            link.Text = "..." + Convert.ToString(p_intTotalPages);
                            link.NavigateUrl = getNewURL(p_intTotalPages);
                        }
                        else if (i == p_intDisplayedPages)
                        {
                            //link.ImageUrl = "~/Images/next1.gif";
                            //link.SkinID = "PagerNext";
                            link.Text = "下一页";
                            link.CssClass = "page-next";
                            link.NavigateUrl = getNewURL(p_intCurrentPage + 1);
                        }
                        cell.Controls.Add(link);
                        row.Cells.Add(cell);
                    }
                }
                else if (p_intCurrentPage > p_intTotalPages - offset && p_intCurrentPage < p_intTotalPages)
                {
                    for (int i = 1; i <= p_intDisplayedPages; i++)
                    {
                        TableCell cell = new TableCell();
                        HyperLink link = new HyperLink();
                        link.EnableTheming = true;
                        if (i == 1)
                        {
                            //link.ImageUrl = "~/Images/fore.gif";
                            //link.SkinID = "PagerFore";
                            link.Text = "上一页";
                            link.CssClass = "page-fore";
                            link.NavigateUrl = getNewURL(p_intCurrentPage - 1);
                        }
                        else if (i == 2)
                        {
                            link.Text = "1...";
                            link.NavigateUrl = getNewURL(1);
                        }
                        else if (i > 2 && i < p_intDisplayedPages)
                        {
                            link.Text = Convert.ToString(p_intTotalPages - p_intDisplayedPages + 1 + i);

                            if (p_intCurrentPage == p_intTotalPages - p_intDisplayedPages + 1 + i)
                            {
                                link.CssClass = "now-page";
                            }
                            else
                            {
                                link.NavigateUrl = getNewURL(p_intTotalPages - p_intDisplayedPages + 1 + i);
                            }
                        }
                        else if (i == p_intDisplayedPages)
                        {
                            //link.ImageUrl = "~/Images/next1.gif";
                            //link.SkinID = "PagerNext";
                            link.Text = "下一页";
                            link.CssClass = "page-next";
                            link.NavigateUrl = getNewURL(p_intCurrentPage + 1);
                        }
                        cell.Controls.Add(link);
                        row.Cells.Add(cell);
                    }
                }

                else if (p_intCurrentPage == p_intTotalPages)
                {
                    for (int i = 1; i <= p_intDisplayedPages; i++)
                    {
                        TableCell cell = new TableCell();
                        HyperLink link = new HyperLink();
                        link.EnableTheming = true;
                        if (i == 1)
                        {
                            //link.ImageUrl = "~/Images/fore.gif";
                            //link.SkinID = "PagerFore";
                            link.Text = "上一页";
                            link.CssClass = "page-fore";
                            link.NavigateUrl = getNewURL(p_intCurrentPage - 1);
                        }
                        else if (i == 2)
                        {
                            link.Text = "1...";
                            link.NavigateUrl = getNewURL(1);
                        }
                        else if (i > 2 && i <= p_intDisplayedPages)
                        {
                            link.Text = Convert.ToString(p_intTotalPages - p_intDisplayedPages + i);
                            if (p_intCurrentPage == p_intTotalPages - p_intDisplayedPages + i)
                            {
                                link.CssClass = "now-page";
                            }
                            else
                            {
                                link.NavigateUrl = getNewURL(p_intTotalPages - p_intDisplayedPages + i);
                            }
                        }
                        cell.Controls.Add(link);
                        row.Cells.Add(cell);
                    }
                }
            }

            if (p_intTotalPages > 1)
            {

                //TableCell pcelljump = new TableCell();
                //pcelljump.Text = "跳转";
                //pcelljump.CssClass = "jump-page";
                //row.Cells.Add(pcelljump);

                //TableCell pcell = new TableCell();
                //HtmlInputText inputBox = new HtmlInputText();

                ////inputBox.Style = "width:50px";
                //inputBox.Style.Value = "width:30px";
                //inputBox.MaxLength = 4;
                //inputBox.Attributes.Add("id", "TxtPageNumber");
                //inputBox.Value = currentPageNo.ToString();
                //inputBox.EnableTheming = true;
                ////inputBox.SkinID = "PagerSearch";
                //inputBox.Attributes.Add("class", "page-text");

                //inputBox.Attributes.Add("onKeyDown", "if(event.keyCode==13) {window.location='" + getCurrentURLWithouKey() + "&page=' + (parseInt(this.value) > 0 ? parseInt(this.value) : 1);return false;}");
                ////inputBox.Attributes.Add("onKeyDown", "if(event.keyCode==13){window.location='http://www.baidu.com';}");

                //pcell.Controls.Add(inputBox);

                //row.Cells.Add(pcell);

                //TableCell pcell2 = new TableCell();
                ////ImageButton ib1 = new ImageButton();


                //HtmlInputImage ib1 = new HtmlInputImage();



                //ib1.EnableTheming = true;
                ////ib1.ImageUrl = "../App_Themes/Images/find.gif";
                //ib1.Src = "../App_Themes/Default/Images/find.gif";
                ////ib1.SkinID = "PagerFind";
                //ib1.Attributes.Add("onClick", "pagenumber = document.getElementById('TxtPageNumber').value; window.location='" + getCurrentURLWithouKey() + "&page='+pagenumber;return false;");
                //pcell2.Controls.Add(ib1);
                //row.Cells.Add(pcell2);
            }
        }

        #region Private Methods

        protected string getCurrentURLWithouKey()
        {
            return HttpContext.Current.Request.FilePath + GetValueQueryStringWithouKey("page");
        }

        protected string getNewURL(int pageNumber)
        {

            return HttpContext.Current.Request.FilePath + GetValueQueryString("page", pageNumber.ToString());
        }

        #endregion

        #region Public Methods

        public void Page_ReLoad()
        {
            try
            {
                this.Table1.Rows.RemoveAt(0);
            }
            catch
            {
            }
            Reader();
        }


        #endregion


        public static string GetValueQueryString(string key, string value)
        {
            string strRequestQueryString = "";

            System.Collections.Specialized.NameValueCollection strRequestQuery = HttpContext.Current.Request.QueryString;
            Dictionary<string, string> odtRequestQuery = new Dictionary<string, string>();



            for (int i = 0; i < strRequestQuery.Count; i++)
            {
                if (strRequestQuery.Get(i).ToString() != "")
                {
                    odtRequestQuery.Add(strRequestQuery.GetKey(i).ToLower(), HttpUtility.UrlEncode(strRequestQuery.Get(i)).ToUpper());
                }
            }

            if (odtRequestQuery.ContainsKey(key))
            {
                odtRequestQuery.Remove(key);
            }
            odtRequestQuery.Add(key, HttpUtility.UrlEncode(value));

            foreach (string strKey in odtRequestQuery.Keys)
            {
                strRequestQueryString += strRequestQueryString == "" ? strKey + "=" + odtRequestQuery[strKey].ToString() : "&" + strKey + "=" + odtRequestQuery[strKey].ToString();
            }


            strRequestQueryString = "?" + strRequestQueryString;

            return strRequestQueryString;
        }

        public static string GetValueQueryStringWithouKey(string key)
        {


            string strRequestQueryString = "";

            System.Collections.Specialized.NameValueCollection strRequestQuery = HttpContext.Current.Request.QueryString;
            Dictionary<string, string> odtRequestQuery = new Dictionary<string, string>();



            for (int i = 0; i < strRequestQuery.Count; i++)
            {
                if (strRequestQuery.Get(i).ToString() != "")
                {
                    odtRequestQuery.Add(strRequestQuery.GetKey(i).ToLower(), HttpUtility.UrlEncode(strRequestQuery.Get(i)).ToUpper());
                }
            }

            if (odtRequestQuery.ContainsKey(key))
            {
                odtRequestQuery.Remove(key);
            }


            foreach (string strKey in odtRequestQuery.Keys)
            {
                strRequestQueryString += strRequestQueryString == "" ? strKey + "=" + odtRequestQuery[strKey].ToString() : "&" + strKey + "=" + odtRequestQuery[strKey].ToString();
            }


            strRequestQueryString = "?" + strRequestQueryString;

            return strRequestQueryString;
        }
    }
}